package com.uniriho.project.lxr0860gzzmb_ic_test;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sdk.devicemanager_0860.ICInterface_0860;
import com.sdk.devicemanager_iscan23y.minbao.ICAuthInterface_23Y;
import com.uniriho.lxr9100ble.service.BLEManager;
import com.uniriho.lxr9100ble.service.BluetoolUtil;
import com.uniriho.lxr9100ble.service.LogUtils;
import com.uniriho.project.lxr0860gzzmb_ic_test.databinding.ActivityMainBinding;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnCheckIC;
    private Button btnPersonCard_Reset;
    private Button btnSam_Reset;
    private Button btnSAM_Write_Info;
    private Button btnUnitCard_Reset;
    private Button btnICWriteFlow;
    private Button btnICReadFlow;
    private Button btnICWriteReadFlow;
    private Button btnP_U_S_test200;
    private Button btnUICClean;
    private Button btnPCBIC_WriteTest;
    private Button btnSelectBtReader;
    private Button btnSelectInnerReader;

    private EditText ETInput;
    private TextView txvInfo;
    private int index = 0;
    private long gl_Flow_Sn;
    public ICAuthInterface_23Y ICAuthI;
    //public SICAuthInterface ICAuthI;


    String viewRestult = new String();

    int samnum_fail = 0;
    int samnum_success = 0;
    int samnum_fail_ryk = 0;
    int samnum_success_ryk = 0;
    int samnum_success_dwk = 0;
    int samnum_fail_dwk = 0;
    SimpleDateFormat sdf = null;
    Date startdate;
    Date enddate;

    String time_start_gl, time_end_gl;
    Date df_start_gl = null, df_finished_gl = null;

    int write_err = 0, read_err = 0, data_err = 0;


    private ActivityMainBinding binding;
    Context mContext;
    AlertDialog dlgscan;



    String mMac = null;// Used to load the 'myapplication' library on application startup.
    private boolean powerState = true;

    private BLEManager bleManager;
    private ICInterface_0860 icInterface0860;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        init();
        String[] filter = new String[]{"DANLING", "COHO", "CMBC", "CGB", "WD1981", "eslink", "W1981",};
        //filter = null; //won't filter
    }

    public void init() {
        txvInfo = (TextView) findViewById(R.id.txvInfo);
        ETInput = (EditText) findViewById(R.id.ETInput);
        btnCheckIC = (Button) findViewById(R.id.btnCheckIC);
        btnPersonCard_Reset = (Button) findViewById(R.id.btnPersonCard_Reset);
        btnSam_Reset = (Button) findViewById(R.id.btnSam_Reset);
        btnSAM_Write_Info = (Button) findViewById(R.id.btnSAM_Write_Info);
        btnUnitCard_Reset = (Button) findViewById(R.id.btnUnitCard_Reset);
        btnICWriteFlow = (Button) findViewById(R.id.btnICWriteFlow);
        btnICReadFlow = (Button) findViewById(R.id.btnICReadFlow);
        btnICWriteReadFlow = (Button) findViewById(R.id.btnICWriteReadFlow);
        btnP_U_S_test200 = (Button) findViewById(R.id.btnU_Stest1000);
        btnUICClean = (Button) findViewById(R.id.btnUICClean);
        btnPCBIC_WriteTest = (Button) findViewById(R.id.btnPCBIC_WriteTest);
        btnSelectBtReader = (Button) findViewById(R.id.btnSelectBtReader);
        btnSelectInnerReader = (Button) findViewById(R.id.btnSelectInnerReader);

        btnUICClean.setOnClickListener(this);
        btnP_U_S_test200.setOnClickListener(this);
        btnICWriteReadFlow.setOnClickListener(this);
        btnICReadFlow.setOnClickListener(this);
        btnICWriteFlow.setOnClickListener(this);
        btnPersonCard_Reset.setOnClickListener(this);
        btnUnitCard_Reset.setOnClickListener(this);
        btnSam_Reset.setOnClickListener(this);
        btnCheckIC.setOnClickListener(this);
        btnSAM_Write_Info.setOnClickListener(this);

        btnPCBIC_WriteTest.setOnClickListener(this);
        btnSelectBtReader.setOnClickListener(this);
        btnSelectInnerReader.setOnClickListener(this);
        //ICAuthI = new SICAuthInterface(MainActivity.this);


        btnCheckIC.setBackgroundColor(Color.LTGRAY);
        btnPersonCard_Reset.setBackgroundColor(Color.LTGRAY);
        btnSam_Reset.setBackgroundColor(Color.LTGRAY);
        btnSAM_Write_Info.setBackgroundColor(Color.LTGRAY);
        btnUnitCard_Reset.setBackgroundColor(Color.LTGRAY);
        btnICWriteFlow.setBackgroundColor(Color.LTGRAY);
        btnICReadFlow.setBackgroundColor(Color.LTGRAY);
        btnICWriteReadFlow.setBackgroundColor(Color.LTGRAY);
        btnP_U_S_test200.setBackgroundColor(Color.LTGRAY);
        btnUICClean.setBackgroundColor(Color.LTGRAY);
        btnPCBIC_WriteTest.setBackgroundColor(Color.LTGRAY);
        btnSelectBtReader.setBackgroundColor(Color.LTGRAY);
        btnSelectInnerReader.setBackgroundColor(Color.LTGRAY);

        BluetoolUtil.getInstance();
        ICAuthI = new ICAuthInterface_23Y();
        ICAuthI.Select_Bt_Channel((short) 1);

    }


    private Handler txvInfoHandler = new Handler() {
        public void handleMessage(final Message msg) {
            switch (msg.what) {
                case 0:
                    txvInfo.setText("OnResum SAM_Reset 成功 :" + "\r\n");
                    break;
                default:
                    txvInfo.setText("OnResum SAM_Reset 失败,返回值:" + msg.what + "\r\n");
                    break;
            }
        }
    };

    private void ButtonEnables(int flg) {
        if (flg == 0) {
            btnCheckIC.setClickable(false);
            btnPersonCard_Reset.setClickable(false);
            btnSam_Reset.setClickable(false);
            btnSAM_Write_Info.setClickable(false);
            btnUnitCard_Reset.setClickable(false);
            btnICWriteFlow.setClickable(false);
            btnICReadFlow.setClickable(false);
            btnICWriteReadFlow.setClickable(false);
            btnP_U_S_test200.setClickable(false);
            btnUICClean.setClickable(false);

        } else {
            btnCheckIC.setClickable(true);
            btnPersonCard_Reset.setClickable(true);
            btnSam_Reset.setClickable(true);
            btnSAM_Write_Info.setClickable(true);
            btnUnitCard_Reset.setClickable(true);
            btnICWriteFlow.setClickable(true);
            btnICReadFlow.setClickable(true);
            btnICWriteReadFlow.setClickable(true);
            btnP_U_S_test200.setClickable(true);
            btnUICClean.setClickable(true);
        }

    }

    String TestAllInfo, SamInfo, UnionInfo;
    private Handler test1000Handler = new Handler() {
        public void handleMessage(final Message msg) {
            switch (msg.what) {
                case 0:
                    SamInfo = "SAM卡复位100次:成功 " + samnum_success + " 次 ,失败 " + samnum_fail + " 次\r\n";
                    txvInfo.setText(SamInfo);
                    break;
                case 1:
                    enddate = new Date(System.currentTimeMillis());
                    long diff = enddate.getTime() - startdate.getTime();
                    SamInfo += "SAM卡复位100次总耗时:" + diff / 1000 + "秒\r\n";
                    txvInfo.setText(SamInfo);
                    break;
                case 2:
                    UnionInfo = SamInfo + "单位卡复位100次:成功 " + samnum_success_dwk + " 次 ,失败 " + samnum_fail_dwk + " 次\r\n";
                    txvInfo.setText(UnionInfo);
                    break;
                case 3:
                    enddate = new Date(System.currentTimeMillis());
                    long diff2 = enddate.getTime() - startdate.getTime();
                    UnionInfo += "单位卡复位100次总耗时:" + diff2 / 1000 + "秒\r\n";
                    txvInfo.setText(UnionInfo);
                    break;
                case 4:
                    txvInfo.setText("成功 " + samnum_success_ryk + " 次 ,失败 " + samnum_fail_ryk + " 次");
                    break;
                case 5:
                    enddate = new Date(System.currentTimeMillis());
                    long diff3 = enddate.getTime() - startdate.getTime();
                    txvInfo.setText("总耗时:" + diff3 / 1000 + "秒");
                    break;
                default:
                    break;
            }
        }
    };

    private Handler setColorHandler = new Handler() {
        public void handleMessage(final Message msg) {
            switch (msg.what) {
                case 0:
                    btnCheckIC.setBackgroundColor(Color.BLUE);
                    break;
                case 1:
                    btnSam_Reset.setBackgroundColor(Color.BLUE);
                    break;
                case 2:
                    btnSAM_Write_Info.setBackgroundColor(Color.BLUE);
                    break;
                case 3:
                    btnPersonCard_Reset.setBackgroundColor(Color.BLUE);
                    break;
                case 4:
                    btnUnitCard_Reset.setBackgroundColor(Color.BLUE);
                    break;
                case 5:
                    btnICWriteFlow.setBackgroundColor(Color.BLUE);
                    break;
                case 6:
                    btnICReadFlow.setBackgroundColor(Color.BLUE);
                    break;
                case 7:
                    btnUICClean.setBackgroundColor(Color.BLUE);
                    break;
                case 9:
                    btnPCBIC_WriteTest.setBackgroundColor(Color.BLUE);
                case 10:
                    btnCheckIC.setBackgroundColor(Color.LTGRAY);
                    break;
                case 11:
                    btnSam_Reset.setBackgroundColor(Color.LTGRAY);
                    break;
                case 12:
                    btnSAM_Write_Info.setBackgroundColor(Color.LTGRAY);
                    break;
                case 13:
                    btnPersonCard_Reset.setBackgroundColor(Color.LTGRAY);
                    break;
                case 14:
                    btnUnitCard_Reset.setBackgroundColor(Color.LTGRAY);
                    break;
                case 15:
                    btnICWriteFlow.setBackgroundColor(Color.LTGRAY);
                    ButtonEnables(1);
                    break;
                case 16:
                    btnICReadFlow.setBackgroundColor(Color.LTGRAY);
                    break;
                case 17:
                    btnP_U_S_test200.setBackgroundColor(Color.LTGRAY);
                case 18:
                    btnUICClean.setBackgroundColor(Color.LTGRAY);
                    break;
                case 19:
                    btnPCBIC_WriteTest.setBackgroundColor(Color.LTGRAY);
                default:
                    break;
            }
        }
    };



    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(mContext, "连接断开！", Toast.LENGTH_LONG).show();
        bleManager.getInstance().disConnectDevice();

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        System.out.println("有按键按下\r\n\r\n");
        BluetoolUtil.getInstance();
        if (v == btnSelectBtReader) {
            System.out.println("选择蓝牙读卡器\r\n");
            ICAuthI.Select_Bt_Channel((short) 1);

            //BLEReader.getInstance().connectGatt(mMac);

        }
        if(v == btnSelectInnerReader){

            ICAuthI.Select_Bt_Channel((short) 0);
        }
        if (v == btnUICClean) {
            ButtonEnables(0);

            setColorHandler.sendEmptyMessage(7);
            byte[] Card_SN = new byte[128];
            byte[] Card_Type = new byte[128];
            byte[] Card_ApplyInfo = new byte[256];
            short ret;
            ret = ICAuthI.SAM_Reset(Card_SN, Card_Type, Card_ApplyInfo);

            if (ret == 0) {
                txvInfo.append("SAM_Reset 成功 \r\n");
            } else {
                System.out.println("SAM_Reset 失败， 返回值:" + ret);
                txvInfo.append("SAM_Reset 失败,返回值:" + ret + "\r\n");
            }

            byte[] password_no = new byte[16];
            byte[] card_dwdm = new byte[64];
            byte[] card_sn = new byte[64];
            byte[] UnitName = new byte[64];
            byte[] UserName = new byte[64];
            byte[] valid_date = new byte[64];
            byte[] invalid_date = new byte[64];

            txvInfo.setText("");
            String outPut;
            SimpleDateFormat sDateFormat;
            String time_start, time_end;
            Date df_start = null, df_finished = null;

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start = sDateFormat.format(new java.util.Date());
            try {
                df_start = sDateFormat.parse(time_start.toString());
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            password_no = "123456".getBytes();
//            ret = ICAuthI.UnitCard_Reset(password_no,
            ret = ICAuthI.UnitCard_Reset(
                    card_dwdm,
                    UnitName,
                    UserName,
                    card_sn,
                    valid_date,
                    invalid_date);

            time_end = sDateFormat.format(new java.util.Date());
            try {
                df_finished = sDateFormat.parse(time_end.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");

            if (ret == 0) {
                txvInfo.append("UnitCard_Reset 成功\r\n");

            } else {
                txvInfo.append("UnitCard_Reset 失败,返回值:" + ret + "\r\n");
                return;
            }

            ret = ICAuthI.IC_Clean_Flow();
            if (ret == 0) {
                txvInfo.append("IC_Clean_Flow 成功");
            } else {
                txvInfo.append("IC_Clean_Flow 失败,返回值:" + ret + "\r\n");
            }
            setColorHandler.sendEmptyMessage(18);
            ButtonEnables(1);

        }

        if (v == btnP_U_S_test200) {
            ButtonEnables(0);
            btnP_U_S_test200.setBackgroundColor(Color.BLUE);
            TestAllInfo = null;
            SamInfo = null;
            UnionInfo = null;
            startdate = new Date(System.currentTimeMillis());
            new Thread(new Runnable() {
                int ret = -1;
                byte[] password_no = new byte[16];
                byte[] card_dwdm = new byte[64];
                byte[] card_sn = new byte[64];
                byte[] UnitName = new byte[64];
                byte[] UserName = new byte[64];
                byte[] valid_date = new byte[64];
                byte[] invalid_date = new byte[64];

                @Override
                public void run() {

                    for (int i = 0; i < 100; i++) {

                        ret = -1;
                        byte[] Card_SN = new byte[128];
                        byte[] Card_Type = new byte[128];
                        byte[] Card_ApplyInfo = new byte[256];
                        ret = ICAuthI.SAM_Reset(Card_SN, Card_Type, Card_ApplyInfo);

                        if (ret == 0) {
                            samnum_success++;
                        } else {
                            samnum_fail++;
                        }
                        test1000Handler.sendEmptyMessage(0);

                    }
                    test1000Handler.sendEmptyMessage(1);

                    for (int i = 0; i < 100; i++) {

                        password_no = "123456".getBytes();
                        ret = -1;

//                        ret = ICAuthI.UnitCard_Reset(password_no,
                        ret = ICAuthI.UnitCard_Reset(
                                card_dwdm,
                                UnitName,
                                UserName,
                                card_sn,
                                valid_date,
                                invalid_date);

                        if (ret == 0) {
                            samnum_success_dwk++;
                        } else {
                            samnum_fail_dwk++;
                        }
                        test1000Handler.sendEmptyMessage(2);

                    }
                    test1000Handler.sendEmptyMessage(3);
                    ButtonEnables(1);
                    setColorHandler.sendEmptyMessage(17);
                  /*
                    for (int i = 0; i < 1000; i++) {
                        short ret, card_type;
                        byte[] password_no = new byte[16];
                        byte[] card_code = new byte[64];
                        byte[] card_dwdm = new byte[64];
                        byte[] card_sn = new byte[64]; //发卡序号
                        byte[] card_name = new byte[64];
                        byte[] valid_date = new byte[64];
                        byte[] invalid_date = new byte[64];

                        String outPut;

                        card_type = 1;
                        password_no = "123456".getBytes();
                        ret = ICAuthI.PersonCard_Reset(card_type, password_no, card_code, card_dwdm, card_sn, card_name, valid_date, invalid_date);
                        if (ret == 0) {
                            samnum_success_ryk++;
                        } else {
                            samnum_fail_ryk++;
                        }
                        handler.sendEmptyMessage(4);

                    }
                    handler.sendEmptyMessage(5);
                    */
                }
            }).start();
        }

        if (v == btnCheckIC) {
			/*byte[] erroInfo = new byte[128];
			int i;
			for (i = 0; i < 10; i ++)
			{
				System.out.println("Test ret: " + ICAuthI.getSAMErrInfo(erroInfo ));
			}*/

            ButtonEnables(0);
            setColorHandler.sendEmptyMessage(0);
            short ret;

            LogUtils.error("IC_Detect_Exist","IC_Detect_Exist((short) 0)");
            ret = ICAuthI.IC_Detect_Exist((short) 0);
            if (ret == 0) {
                txvInfo.setText("IC_Detect_Exist结果:卡槽有卡");
            } else {
                txvInfo.setText("IC_Detect_Exist结果:无卡 , ret:" + ret);
            }
            setColorHandler.sendEmptyMessage(10);
            ButtonEnables(1);
        }

        if (v == btnSam_Reset) {
            ButtonEnables(0);

            setColorHandler.sendEmptyMessage(1);
            byte[] Card_SN = new byte[128];
            byte[] Card_Type = new byte[128];
            byte[] Card_ApplyInfo = new byte[256];
            short ret;
            try {

                ret = ICAuthI.SAM_Reset(Card_SN, Card_Type, Card_ApplyInfo);
                if (ret == 0) {
                    String outPut = new String(Card_ApplyInfo);
                    System.out.println("SAM_Reset 成功");
                    txvInfo.setText("SAM_Reset 成功 :" + outPut + "\r\n");

                } else {
                    System.out.println("SAM_Reset 失败， 返回值:" + ret);
                    txvInfo.setText("SAM_Reset 失败,返回值:" + ret + "\r\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            setColorHandler.sendEmptyMessage(11);
            ButtonEnables(1);
        }

        if (v == btnSAM_Write_Info) {
            ButtonEnables(0);

            setColorHandler.sendEmptyMessage(2);
            byte[] Card_Data_Buff = new byte[128];
            int Card_Data_Len = 64;
            short ret = -1;
            String outPut;

            System.out.println("数据长度:" + ETInput.length());
            Card_Data_Buff = ETInput.getText().toString().getBytes();
            outPut = new String(Card_Data_Buff);
            System.out.println("数据内容:" + outPut);

            ret = ICAuthI.SAM_Write_Info(Card_Data_Buff, Card_Data_Len);

            System.out.println("写数据结果:" + ret);
            if (ret == 0)
                txvInfo.setText("SAM_Write_Info 成功 \r\n");
            else
                txvInfo.setText("SAM_Write_Info 失败,返回值:" + ret + "\r\n");


            setColorHandler.sendEmptyMessage(12);
            ButtonEnables(1);
        }

        if (v == btnPersonCard_Reset) {
            ButtonEnables(0);

            setColorHandler.sendEmptyMessage(3);
            short ret, card_type;
            byte[] password_no = new byte[16];
            byte[] card_code = new byte[64];
            byte[] card_dwdm = new byte[64];
            byte[] card_sn = new byte[64];
            byte[] card_name = new byte[64];
            byte[] valid_date = new byte[64];
            byte[] invalid_date = new byte[64];

            String outPut;

            card_type = 1;
            password_no = "123456".getBytes();
            ret = ICAuthI.PersonCard_Reset(card_type, password_no, card_code, card_dwdm, card_sn, card_name, valid_date, invalid_date);

            if (ret == 0) {
                outPut = new String(card_code) + "\r\n";
                outPut += new String(card_dwdm) + "\r\n";
                outPut += new String(card_sn) + "\r\n";
                outPut += new String(card_name) + "\r\n";
                outPut += new String(valid_date) + "\r\n";
                outPut += new String(invalid_date) + "\r\n";
                txvInfo.setText("PersonCard_Reset 成功:" + outPut);

            } else {
                txvInfo.setText("PersonCard_Reset 失败,返回值:" + ret + "\r\n");
            }


            setColorHandler.sendEmptyMessage(13);
            ButtonEnables(1);
        }

        if (v == btnUnitCard_Reset) {
            ButtonEnables(0);
            setColorHandler.sendEmptyMessage(4);
            short ret;
            byte[] password_no = new byte[16];
            byte[] card_dwdm = new byte[64];
            byte[] card_sn = new byte[64];
            byte[] UnitName = new byte[64];
            byte[] UserName = new byte[64];
            byte[] valid_date = new byte[64];
            byte[] invalid_date = new byte[64];

            txvInfo.setText("");
            String outPut;
            SimpleDateFormat sDateFormat;
            String time_start, time_end;
            Date df_start = null, df_finished = null;

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start = sDateFormat.format(new java.util.Date());
            try {
                df_start = sDateFormat.parse(time_start.toString());
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            password_no = "123456".getBytes();
//            ret = ICAuthI.UnitCard_Reset(password_no,
            ret = ICAuthI.UnitCard_Reset(
                    card_dwdm,
                    UnitName,
                    UserName,
                    card_sn,
                    valid_date,
                    invalid_date);

            time_end = sDateFormat.format(new java.util.Date());
            try {
                df_finished = sDateFormat.parse(time_end.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");

            if (ret == 0) {
                //outPut = my_Hex_dump(card_dwdm,)
                outPut = new String(card_dwdm) + "\r\n";
                outPut += new String(card_sn) + "\r\n";
                outPut += new String(UnitName) + "\r\n";
                outPut += new String(valid_date) + "\r\n";
                outPut += new String(invalid_date) + "\r\n";
                txvInfo.append("UnitCard_Reset 成功:" + outPut);

            } else {
                byte[] errorInfo = new byte[64];
                ICAuthI.getSAMErrInfo(errorInfo);
                outPut = new String(errorInfo);
                txvInfo.append("UnitCard_Reset 失败,返回值:" + ret + ",erroInfo:" + outPut + "\r\n");
            }

            setColorHandler.sendEmptyMessage(14);
            ButtonEnables(1);
        }

        if (v == btnICWriteFlow) {
            ButtonEnables(0);

            setColorHandler.sendEmptyMessage(5);
            txvInfo.setText("");
            Thread btICwriteThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    short ret;
                    byte[] password_no = new byte[16];
                    byte[] card_dwdm = new byte[64];
                    byte[] card_sn = new byte[64];
                    byte[] UnitName = new byte[64];
                    byte[] UserName = new byte[64];
                    byte[] valid_date = new byte[64];
                    byte[] invalid_date = new byte[64];



                    String outPut;
                    SimpleDateFormat sDateFormat;
                    String time_start, time_end;
                    Date df_start = null, df_finished = null;

                    sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    time_start = sDateFormat.format(new java.util.Date());
                    try {
                        df_start = sDateFormat.parse(time_start.toString());
                    } catch (ParseException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    password_no = "123456".getBytes();
//                    ret = ICAuthI.UnitCard_Reset(password_no,
                    ret = ICAuthI.UnitCard_Reset(
                            card_dwdm,
                            UnitName,
                            UserName,
                            card_sn,
                            valid_date,
                            invalid_date);

                    time_end = sDateFormat.format(new java.util.Date());
                    try {
                        df_finished = sDateFormat.parse(time_end.toString());
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");
                    if (ret == 0) {
                        //outPut = my_Hex_dump(card_dwdm,)
                        outPut = new String(card_dwdm) + "\r\n";
                        outPut += new String(card_sn) + "\r\n";
                        outPut += new String(UnitName) + "\r\n";
                        outPut += new String(valid_date) + "\r\n";
                        outPut += new String(invalid_date) + "\r\n";
                        //txvInfo.append("UnitCard_Reset 成功:" + outPut);
                        viewRestult = "UnitCard_Reset 成功:" + outPut;
                        update_textView.post(new Runnable() {
                            @Override
                            public void run() {
                                txvInfo.append(viewRestult);
                            }
                        });


                    } else {
                        byte[] errorInfo = new byte[64];
                        ICAuthI.getSAMErrInfo(errorInfo);
                        outPut = new String(errorInfo);
                        viewRestult = "UnitCard_Reset 失败,返回值:" + ret + ",erroInfo:" + outPut + "\r\n";
                        update_textView.post(new Runnable() {
                            @Override
                            public void run() {
                                txvInfo.append(viewRestult);
                            }
                        });
                    }

                    setColorHandler.sendEmptyMessage(5);

                    String echo = "0201704181234";
                    String Data_Buff = "1111111111111111222222222222222233333333333333334444444444444444";
                    byte[] data_buf = new byte[10240];
                    int[] dlen = new int[2];
                    long[] flow_index = new long[2];

                    ret = ICAuthI.IC_Read_DirNum(flow_index);
                    if (ret == 0) {
                        gl_Flow_Sn = flow_index[0];
                    } else {

                    }
                    Log.d("ICAuthI", "flow_index:" + gl_Flow_Sn);
                    ret = ICAuthI.IC_Write_Flow((short) 2, (short) (gl_Flow_Sn * 2 + 1), echo.getBytes(),
                            Data_Buff.getBytes(),
                            Data_Buff.length());

                    ret = ICAuthI.IC_Read_DirNum(flow_index);
                    if (ret == 0) {
                        gl_Flow_Sn = flow_index[0];
                    } else {

                    }
                    Log.d("ICAuthI", "flow_index:" + gl_Flow_Sn);
                    Data_Buff = "2222222222222222333333333333333344444444444444445555555555555555";
                    ret = ICAuthI.IC_Write_Flow((short) 2, (short) (gl_Flow_Sn * 2 + 1), echo.getBytes(),
                            Data_Buff.getBytes(),
                            Data_Buff.length());

                    ret = ICAuthI.IC_Read_DirNum(flow_index);
                    if (ret == 0) {
                        gl_Flow_Sn = flow_index[0];
                    } else {

                    }
                    Log.d("ICAuthI", "flow_index:" + gl_Flow_Sn);
                    Data_Buff = "3333333333333333444444444444444455555555555555556666666666666666";
                    ret = ICAuthI.IC_Write_Flow((short) 2, (short) (gl_Flow_Sn * 2 + 1), echo.getBytes(),
                            Data_Buff.getBytes(),
                            Data_Buff.length());

                    ret = ICAuthI.IC_Read_DirNum(flow_index);
                    if (ret == 0) {
                        gl_Flow_Sn = flow_index[0];
                    } else {

                    }
                    Log.d("ICAuthI", "flow_index:" + gl_Flow_Sn);
                    Data_Buff = "4444444444444444555555555555555566666666666666667777777777777777";
                    ret = ICAuthI.IC_Write_Flow((short) 2, (short) (gl_Flow_Sn * 2 + 1), echo.getBytes(),
                            Data_Buff.getBytes(),
                            Data_Buff.length());

                    ret = ICAuthI.IC_Read_DirNum(flow_index);
                    if (ret == 0) {
                        gl_Flow_Sn = flow_index[0];
                    } else {

                    }
                    Log.d("ICAuthI", "flow_index:" + gl_Flow_Sn);
                    Data_Buff = "5555555555555555666666666666666677777777777777778888888888888888";
                    ret = ICAuthI.IC_Write_Flow((short) 2, (short) (gl_Flow_Sn * 2 + 1), echo.getBytes(),
                            Data_Buff.getBytes(),
                            Data_Buff.length());


                    ret = ICAuthI.IC_Read_AllDirContext(data_buf, dlen);
                    Log.d("ICAuthI", my_Hex_dump(data_buf, dlen[0]));

                    ret = ICAuthI.IC_Change_DirContext(2, "1205001010101".getBytes());        //入：数据流序号


                    ret = ICAuthI.IC_Read_AllDirContext(data_buf, dlen);
                    Log.d("ICAuthI", my_Hex_dump(data_buf, dlen[0]));

                    //txvInfo.append("IC_Write_Flow ret:"+ ret + "\r\n");
                    viewRestult = "IC_Write_Flow ret:" + ret + "\r\n";
                    update_textView.post(new Runnable() {
                        @Override
                        public void run() {
                            txvInfo.append(viewRestult);
                        }
                    });
                    if (ret == 0) {
                        //txvInfo.append("写单位卡 成功 \r\n");
                        viewRestult = "写单位卡 成功 \r\n";
                        update_textView.post(new Runnable() {
                            @Override
                            public void run() {
                                txvInfo.append(viewRestult);
                            }
                        });
                    } else {
                        //txvInfo.append("写单位卡 失败,返回值:" + ret + "\r\n");
                        viewRestult = "写单位卡 失败,返回值:" + ret + "\r\n";
                        update_textView.post(new Runnable() {
                            @Override
                            public void run() {
                                txvInfo.append(viewRestult);
                            }
                        });
                    }

                    setColorHandler.sendEmptyMessage(15);

                }
            });
            btICwriteThread.start();


        }

        if (v == btnICReadFlow) {
            ButtonEnables(0);
            SimpleDateFormat sDateFormat;
            String time_start, time_end;
            Date df_start = null, df_finished = null;

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSS");
            time_start = sDateFormat.format(new java.util.Date());
            try {
                df_start = sDateFormat.parse(time_start.toString());
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            setColorHandler.sendEmptyMessage(6);
            short ret = -1;
            long[] Flow_Sn = new long[2];    //出：数据流序号
            byte[] Data_Buff = new byte[0xFFFFF];    //出：数据内容
            int[] Data_Len = new int[1];
            String Data_disp = "";

            long[] flow_index = new long[2];
            ret = ICAuthI.IC_Read_DirNum(flow_index);
            if (ret == 0) {
                gl_Flow_Sn = flow_index[0];
            } else {
                gl_Flow_Sn = 0;
            }

            ret = ICAuthI.IC_Read_Flow((short) 2, (short) (gl_Flow_Sn * 2 - 1), (short) 0x30, Flow_Sn, Data_Buff, Data_Len);

            Data_Buff[Data_Len[0]] = '\0';
            byte[] Data_Buff_True = new byte[Data_Len[0]];    //出：数据内容
            System.arraycopy(Data_Buff, 0, Data_Buff_True, 0, Data_Len[0]);
            Data_disp = new String(Data_Buff_True);
            txvInfo.setText("IC_Read_Flow ret:" + ret + "\r\n" + "Flow_Sn:" + Flow_Sn[0] + ";Data:" + Data_disp + "\r\n" + "Data len:" + Data_Len[0] + "\r\n");

            if (ret == 0)
                txvInfo.append("读单位卡 成功 \r\n");
            else
                txvInfo.append("读单位卡 失败,返回值:" + ret + "\r\n");

            time_end = sDateFormat.format(new java.util.Date());
            try {
                df_finished = sDateFormat.parse(time_end.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");
            txvInfo.append("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");
            setColorHandler.sendEmptyMessage(16);
            ButtonEnables(1);
        }

        if (v == btnICWriteReadFlow) {
            short ret;
            long[] flow_index = new long[2];

            byte[] password_no = new byte[16];
            byte[] card_dwdm = new byte[64];
            byte[] card_sn = new byte[64];
            byte[] UnitName = new byte[64];
            byte[] UserName = new byte[64];
            byte[] valid_date = new byte[64];
            byte[] invalid_date = new byte[64];

            txvInfo.setText("");
            String outPut;
            SimpleDateFormat sDateFormat;
            String time_start, time_end;
            Date df_start = null, df_finished = null;

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start = sDateFormat.format(new java.util.Date());
            try {
                df_start = sDateFormat.parse(time_start.toString());
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            password_no = "123456".getBytes();
//            ret = ICAuthI.UnitCard_Reset(password_no,
            ret = ICAuthI.UnitCard_Reset(
                    card_dwdm,
                    UnitName,
                    UserName,
                    card_sn,
                    valid_date,
                    invalid_date);

            time_end = sDateFormat.format(new java.util.Date());
            try {
                df_finished = sDateFormat.parse(time_end.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");

            if (ret == 0) {
                //outPut = my_Hex_dump(card_dwdm,)
                outPut = new String(card_dwdm) + "\r\n";
                outPut += new String(card_sn) + "\r\n";
                outPut += new String(UnitName) + "\r\n";
                outPut += new String(valid_date) + "\r\n";
                outPut += new String(invalid_date) + "\r\n";
                txvInfo.append("UnitCard_Reset 成功:" + outPut);

            } else {
                byte[] errorInfo = new byte[64];
                ICAuthI.getSAMErrInfo(errorInfo);
                outPut = new String(errorInfo);
                txvInfo.append("UnitCard_Reset 失败,返回值:" + ret + ",erroInfo:" + outPut + "\r\n");
                return;
            }

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start_gl = sDateFormat.format(new java.util.Date());
            try {
                df_start_gl = sDateFormat.parse(time_start_gl);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Log.d("ICAuthI", "df_start_gl:" + df_start_gl.toString());

            write_err = 0;
            read_err = 0;
            data_err = 0;
            ButtonEnables(0);
            btnICWriteReadFlow.setBackgroundColor(Color.BLUE);
            index = 0;
            ret = ICAuthI.IC_Read_DirNum(flow_index);
            if (ret == 0) {
                index = (int) flow_index[0];
                gl_Flow_Sn = flow_index[0];
            } else {
                index = 0;
            }
            txvInfo.post(update_thread);
        }

        if (v == btnPCBIC_WriteTest) {
            short ret;
            long[] flow_index = new long[2];

            byte[] password_no = new byte[16];
            byte[] card_dwdm = new byte[64];
            byte[] card_sn = new byte[64];
            byte[] UnitName = new byte[64];
            byte[] UserName = new byte[64];
            byte[] valid_date = new byte[64];
            byte[] invalid_date = new byte[64];

            txvInfo.setText("");
            String outPut;
            SimpleDateFormat sDateFormat;
            String time_start, time_end;
            Date df_start = null, df_finished = null;

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start = sDateFormat.format(new java.util.Date());
            try {
                df_start = sDateFormat.parse(time_start.toString());
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            password_no = "123456".getBytes();
//            ret = ICAuthI.UnitCard_Reset(password_no,
            ret = ICAuthI.UnitCard_Reset(
                    card_dwdm,
                    UnitName,
                    UserName,
                    card_sn,
                    valid_date,
                    invalid_date);

            time_end = sDateFormat.format(new java.util.Date());
            try {
                df_finished = sDateFormat.parse(time_end.toString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("耗时:" + (df_finished.getTime() - df_start.getTime()) + "毫秒");

            if (ret == 0) {
                //outPut = my_Hex_dump(card_dwdm,)
                outPut = new String(card_dwdm) + "\r\n";
                outPut += new String(card_sn) + "\r\n";
                outPut += new String(UnitName) + "\r\n";
                outPut += new String(valid_date) + "\r\n";
                outPut += new String(invalid_date) + "\r\n";
                txvInfo.append("UnitCard_Reset 成功:" + outPut);

            } else {
                byte[] errorInfo = new byte[64];
                ICAuthI.getSAMErrInfo(errorInfo);
                outPut = new String(errorInfo);
                txvInfo.append("UnitCard_Reset 失败,返回值:" + ret + ",erroInfo:" + outPut + "\r\n");
                return;
            }

            sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time_start_gl = sDateFormat.format(new java.util.Date());
            try {
                df_start_gl = sDateFormat.parse(time_start_gl);
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Log.d("ICAuthI", "df_start_gl:" + df_start_gl.toString());

            write_err = 0;
            read_err = 0;
            data_err = 0;
            ButtonEnables(0);
            btnPCBIC_WriteTest.setBackgroundColor(Color.BLUE);
            index = 0;
            ret = ICAuthI.IC_Read_DirNum(flow_index);
            if (ret == 0) {
                index = (int) flow_index[0];
                gl_Flow_Sn = flow_index[0];
            } else {
                index = 0;
            }
            txvInfo.post(update_thread);


        }

    }

    // 创建一个handler，内部完成处理消息方法
    Handler update_textView = new Handler();// 不加这个分号则不能

    // 创建一个handler，内部完成处理消息方法
    Handler test_handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // super.handleMessage(msg);
            // 显示进度条
            index += 1;
				/*try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
            test_handler.post(update_thread);
        }
    };// 不加这个分号则不能自动添加代码

    // 创建一个handler，内部完成处理消息方法
    Handler pcb_rw_handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // super.handleMessage(msg);
            // 显示进度条
            index += 1;
				/*try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
            pcb_rw_handler.post(pcb_ic_cicle_rw);
        }
    };// 不加这个分号则不能自动添加代码

    Runnable update_thread = new Runnable() {

        public void run() {
            Message msg = test_handler.obtainMessage();
            msg.arg1 = index;
            short ret;
            int i;
            short yType;
            // 重新把进程加入到进程队列中
            if (msg.arg1 > gl_Flow_Sn + 100) {
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                time_end_gl = sDateFormat.format(new java.util.Date());

                try {
                    df_finished_gl = sDateFormat.parse(time_end_gl);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                test_handler.removeCallbacks(update_thread);
                txvInfo.append("测试结束");
                btnICWriteReadFlow.setBackgroundColor(Color.LTGRAY);
                txvInfo.append("写卡失败次数: " + write_err + " 读卡失败次数:" + read_err + "数据错误次数:" + data_err + "\r\n");
                txvInfo.append("耗时:" + (df_finished_gl.getTime() - df_start_gl.getTime()) + "毫秒");
                ButtonEnables(1);
                return;
            } else {
                txvInfo.setText("测试过程:" + msg.arg1);
                Log.d("ICAuthI", "write 1");
                String echo = "0201906181234";
                String Data_Buff = "1111111111111111111111111111111111111111111111111111111111111111";

                Data_Buff = ETInput.getText().toString().substring(0, 63);

                yType = (short) ((index * 2) + 1);
                txvInfo.append("\r\nyType:" + yType);
                ret = ICAuthI.IC_Write_Flow((short) 2, (short) ((index * 2) + 1), echo.getBytes(),
                        Data_Buff.getBytes(),
                        Data_Buff.length());
                if (ret != 0)
                    write_err++;

                txvInfo.append("\r\nIC_Write_Flow ret:" + ret + "\r\n");
                Log.d("ICAuthI", "read 0x30");
                long[] Flow_Sn = new long[2];    //出：数据流序号
                byte[] Data_Buff_Byte = new byte[0xFFFFF];    //出：数据内容
                int[] Data_Len = new int[1];
                String Data_disp = "";

                ret = ICAuthI.IC_Read_Flow((short) 2, (short) ((index * 2) + 1), (short) 0x30, Flow_Sn, Data_Buff_Byte, Data_Len);

                Data_Buff_Byte[Data_Len[0]] = '\0';
                byte[] Data_Buff_True = new byte[Data_Len[0]];    //出：数据内容
                System.arraycopy(Data_Buff_Byte, 0, Data_Buff_True, 0, Data_Len[0]);
                Data_disp = new String(Data_Buff_True);

                if (ret != 0) {
                    System.out.println("ICAuthI.IC_Read_Flow err ret=" + ret);
                    read_err++;
                }
                Log.d("ICAuthI", Data_disp);
                if (Data_disp.equals(Data_Buff) == true) {

                    txvInfo.append("IC_Read_Flow ret:" + ret + "\r\n" + "Flow_Sn:" + Flow_Sn[0] + ";Data:" + Data_disp + "\r\n" + "Data len:" + Data_Len[0] + "\r\n");
                } else {
                    txvInfo.append("IC_Read_Flow ret:" + ret + "\r\n" + "Flow_Sn:" + Flow_Sn[0] + ";Data:" + "err" + "\r\n" + "Data len:" + Data_Len[0] + "\r\n");
                    data_err++;
                }
                txvInfo.append("写卡失败次数: " + write_err + " 读卡失败次数:" + read_err + "数据错误次数:" + data_err + "\r\n");
            }
            test_handler.sendMessage(msg);
        }
    };


    Runnable pcb_ic_cicle_rw = new Runnable() {

        public void run() {
            Message msg = pcb_rw_handler.obtainMessage();
            msg.arg1 = index;
            short ret;
            int i;
            short yType;
            // 重新把进程加入到进程队列中
            if (msg.arg1 > gl_Flow_Sn + 100) {

                pcb_rw_handler.removeCallbacks(pcb_ic_cicle_rw);
                txvInfo.append("测试结束");
                btnICWriteReadFlow.setBackgroundColor(Color.LTGRAY);
                txvInfo.append("写卡失败次数: " + write_err + " 读卡失败次数:" + read_err + "数据错误次数:" + data_err + "\r\n");
                ButtonEnables(1);
                return;
            } else {
                txvInfo.setText("测试过程:" + msg.arg1);

                String echo = "0201704181234";
                String Data_Buff = "1111111111111111222222222222222233333333333333334444444444444444";

                yType = (short) ((index * 2) + 1);
                txvInfo.append("\r\nyType:" + yType);
                ret = ICAuthI.IC_Write_Flow((short) 2, (short) ((index * 2) + 1), echo.getBytes(),
                        Data_Buff.getBytes(),
                        Data_Buff.length());

                if (ret != 0)
                    write_err++;

                txvInfo.append("\r\nIC_Write_Flow ret:" + ret + "\r\n");

                long[] Flow_Sn = new long[2];    //出：数据流序号
                byte[] Data_Buff_Byte = new byte[0xFFFFF];    //出：数据内容
                int[] Data_Len = new int[1];
                String Data_disp = "";

                ret = ICAuthI.IC_Read_Flow((short) 2, (short) ((index * 2) + 1), (short) 0x30, Flow_Sn, Data_Buff_Byte, Data_Len);

                Data_Buff_Byte[Data_Len[0]] = '\0';
                byte[] Data_Buff_True = new byte[Data_Len[0]];    //出：数据内容
                System.arraycopy(Data_Buff_Byte, 0, Data_Buff_True, 0, Data_Len[0]);
                Data_disp = new String(Data_Buff_True);

                if (ret != 0) {
                    System.out.println("ICAuthI.IC_Read_Flow err ret=" + ret);
                    read_err++;
                }
                if (Data_disp.equals(Data_Buff) == true) {
                    txvInfo.append("IC_Read_Flow ret:" + ret + "\r\n" + "Flow_Sn:" + Flow_Sn[0] + ";Data:" + Data_disp + "\r\n" + "Data len:" + Data_Len[0] + "\r\n");
                } else {
                    txvInfo.append("IC_Read_Flow ret:" + ret + "\r\n" + "Flow_Sn:" + Flow_Sn[0] + ";Data:" + "err" + "\r\n" + "Data len:" + Data_Len[0] + "\r\n");
                    data_err++;
                }
                txvInfo.append("写卡失败次数: " + write_err + " 读卡失败次数:" + read_err + "数据错误次数:" + data_err + "\r\n");
            }
            pcb_rw_handler.sendMessage(msg);
        }
    };

    private String my_Hex_dump(byte[] bytes, int length) {
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < length; i++) {
            sb.append(String.format("%02x ", bytes[i]));
        }
        return sb.toString();
    }


    /**
     * @功能: BCD码转为10进制串(阿拉伯数据)
     * @参数: BCD码
     * @结果: 10进制串
     */
    public static String bcd2Str(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
                .toString().substring(1) : temp.toString();
    }

    /**
     * @功能: 10进制串转为BCD码
     * @参数: 10进制串
     * @结果: BCD码
     */
    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

    private boolean testState = false;
    private byte[] recvData = new byte[20];
    private  int readLen = 10;
    public void InterfaceTest(View view) {
        /*testState =!testState;
        if (!testState) {
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (testState){
                    int readBleData = BluetoolUtil2.getInstance().readBleData(recvData, readLen);
                    if (readBleData == -1) {
                        Log.e("readBleData","数据为空");
                    }
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();*/
        Log.e("InterfaceTest","click");
        BluetoolUtil.getInstance();
        ButtonEnables(0);
        setColorHandler.sendEmptyMessage(0);
        if (powerState) {
            powerState = false;
            short ret;
            ret = ICAuthI.ICC_interface_power((short) 1);
            LogUtils.error("InterfaceTest","ICC_interface_power((short) 1)");
            if (ret == 0) {
                txvInfo.setText("ICC_interface_powerON结果:成功");

            } else {
                txvInfo.setText("ICC_interface_powerON结果:失败 , ret:" + ret);
            }
        }else{
            powerState = true;
            short ret = 0;
            LogUtils.error("InterfaceTest","ICC_interface_power((short) 0)");
            ret = ICAuthI.ICC_interface_power((short) 0);
            if (ret == 0) {
                txvInfo.setText("ICC_interface_powerOFF结果:成功");

            } else {
                txvInfo.setText("ICC_interface_powerOFF结果:失败 , ret:" + ret);
            }
        }

        setColorHandler.sendEmptyMessage(10);
        ButtonEnables(1);
    }
}